package quanlydoanra.themmoi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.faces.model.SelectItem;

import dbutil.ConnectDB;
public class TruyXuat {
	private SelectItem[] listItemLinhVuc = null;

	public SelectItem[] getListItemLinhVuc() {
		return this.listItemLinhVuc();
	}

	public void setListItemLinhVuc(SelectItem[] listItemLinhVuc) {
		this.listItemLinhVuc = listItemLinhVuc;
	}

	
	private boolean distincLinhVuc(List<LinhVuc> list, LinhVuc linhvuc){
		boolean result = true;
		for (LinhVuc l : list) {
			if(l.getLinhVuc().equals(linhvuc.getLinhVuc())){
				result = false;
				break;
			}
			
		}
		return result;
	}
	// lay ra danh cac nhom chuc nang
	/**************************************
	 * Method name		: dsLinhVuc
	 * Return type		: List<LinhVuc>
	 * Description		:
	 * Created date		: Aug 15, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public List<LinhVuc> dsLinhVuc() {
		List<LinhVuc> listLinhVuc = new ArrayList<LinhVuc>();
		try{
		String SQL = "SELECT DISTINCT  id,linhvuc FROM mucdich";
		ArrayList array = ConnectDB.getDataTypeList(SQL);
		System.out.println("array "+array.size());
		System.out.println(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			for (int i = 0; i < array.size(); i++) {
				LinhVuc LV = new LinhVuc();
				hash = (HashMap<String, Object>) array.get(i);
				LV.setId(Integer.parseInt(hash.get("id").toString()));
				LV.setLinhVuc(hash.get("linhvuc").toString());
				if (this.distincLinhVuc(listLinhVuc, LV) == true){
					listLinhVuc.add(LV);
				}
				}			
		}
		}
		catch(Exception ex){
			System.out.println("Loi");
		}		
		return listLinhVuc;
	}

	// tao cac selectItem tuong ung
	/**************************************
	 * Method name		: listItemLinhVuc
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 15, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public SelectItem[] listItemLinhVuc() {
		List<LinhVuc> listFunct = this.dsLinhVuc();
		SelectItem[] arrItem = new SelectItem[listFunct.size()];
		for (int i = 0; i < listFunct.size(); i++) {
			LinhVuc FD = listFunct.get(i);
			arrItem[i] = new SelectItem(FD.getLinhVuc(), FD.getLinhVuc());
		}
		return arrItem;
	}
	
}
